package cn.itcast.orders.controller;


import cn.itcast.orders.dto.OrdersCancelDTO;
import cn.itcast.orders.dto.OrdersPageQueryDTO;
import cn.itcast.orders.dto.OrdersPaymentDTO;
import cn.itcast.orders.dto.OrdersSubmitDTO;
import cn.itcast.orders.pojo.HistoricalOrders;
import cn.itcast.orders.service.OrderService;
import cn.itcast.orders.vo.OrderPaymentVO;
import cn.itcast.orders.vo.OrderStatisticsVO;
import cn.itcast.orders.vo.OrderSubmitVO;
import cn.itcast.orders.vo.OrderVO;
import cn.itcast.result.PageBean;
import cn.itcast.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/admin/order")
@Api(tags = "订单相关")
public class OrderController {

    @Autowired
    private OrderService orderService;


    /**
     * 订单搜索
     *
     * @param dto
     * @return
     */
    @ApiOperation("订单搜索")
    @GetMapping("/conditionSearch")
    public Result<PageBean> getList(OrdersPageQueryDTO dto){
        log.info("订单搜索：{}",dto);
        PageBean pageBean = orderService.getByconditionSearch(dto);

        return Result.success(pageBean);
    }

    /**
     * 各个状态的订单数量统计
     *
     * @return
     */
    @ApiOperation("各个状态的订单数量统计")
    @GetMapping("/statistics")
    public Result<OrderStatisticsVO> statisticsOrderStatus(){
        log.info("统计订单状态");
        OrderStatisticsVO vo =  orderService.statisticsOrderStatus();

        return Result.success(vo);
    }

    /**
     * 查询订单详情
     *
     * @param id
     * @return
     */
    @ApiOperation("查询订单详情")
    @GetMapping("/details/{id}")
    public Result<OrderVO> getById(@PathVariable Integer id){
        log.info("查询订单详情：{}",id);
        OrderVO orderVO = orderService.getById(id);

        return Result.success(orderVO);
    }


    /**
     * 接单
     *
     * @return
     */
    @ApiOperation("接单")
    @PutMapping("/confirm")
    public Result<String> orderConfirm(@RequestBody Map<String,Integer> map){
        log.info("接单:{}",map);
        orderService.orderConfirm(map.get("id"));

        return Result.success();
    }


    /**
     * 拒单
     *
     * @param map
     * @return
     */
    @ApiOperation("拒单")
    @PutMapping("/rejection")
    public Result orderRejection(@RequestBody Map<String, Object> map){
        log.info("拒单：{}",map);
        orderService.orderRejection(map);

        return Result.success();
    }

    /**
     * 取消订单
     *
     * @param dto
     * @return
     */
    @ApiOperation("取消订单")
    @PutMapping("/cancel")
    public Result<String> orderCancel(@RequestBody OrdersCancelDTO dto){
        log.info("取消订单:{}",dto);
        orderService.orderCancel(dto);

        return Result.success();
    }

    /**
     * 派送订单
     *
     * @param id
     * @return
     */
    @ApiOperation("派送订单")
    @PutMapping("/delivery/{id}")
    public Result<String> orderdDlivery(@PathVariable Integer id){
        log.info("派送订单:{}",id);
        orderService.orderdDlivery(id);

        return Result.success();
    }

    /**
     * 完成订单
     *
     * @param id
     * @return
     */
    @ApiOperation("完成订单")
    @PutMapping("/complete/{id}")
    public Result<String> orderdComplete(@PathVariable Integer id){
        log.info("完成订单:{}",id);
        orderService.orderdComplete(id);

        return Result.success();
    }

    //===
    //==
    //=



















    /**
     * 用户下单
     *
     * @param dto
     * @return
     */
    @ApiOperation("用户下单")
    @PostMapping("/submit")
    public Result<OrderSubmitVO> getOrderSubmit(@RequestBody OrdersSubmitDTO dto) {
        log.info("用户下单:{}", dto);
        OrderSubmitVO orderSubmitVO = orderService.getOrderSubmit(dto);

        return Result.success(orderSubmitVO);
    }

    /**
     * 订单支付
     *
     * @param ordersPaymentDTO
     * @return
     */
    @PutMapping("/payment")
    @ApiOperation("订单支付")
    public Result<OrderPaymentVO> payment(@RequestBody OrdersPaymentDTO ordersPaymentDTO) throws Exception {
        log.info("订单支付：{}", ordersPaymentDTO);
        OrderPaymentVO orderPaymentVO = orderService.payment(ordersPaymentDTO);
        log.info("生成预支付交易单：{}", orderPaymentVO);
        return Result.success(orderPaymentVO);
    }

    /**
     * 历史订单查询
     *
     * @param dto
     * @return
     */
    @ApiOperation("历史订单查询")
    @GetMapping("historyOrders")
    public Result<PageBean> getHistoricalOrders(OrdersPageQueryDTO dto){
        log.info("历史订单查询：{}",dto);
        PageBean pageBean = orderService.getHistoricalOrders(dto);

        return Result.success(pageBean);
    }


    /**
     * 查询订单详情
     *
     * @param id
     * @return
     */
    @ApiOperation("查询订单详情")
    @GetMapping("/orderDetail/{id}")
    public Result<HistoricalOrders> getOrderDetail(@PathVariable Integer id){
        log.info("查询订单详情：{}",id);
        HistoricalOrders historicalOrders = orderService.getOrderDetailById(id);
        return Result.success(historicalOrders);
    }

    /**
     * 取消订单
     *
     * @param id
     * @return
     */
    @ApiOperation("取消订单")
    @PutMapping("/cancel/{id}")
    public Result<String> cancelOrder(@PathVariable Integer id){
        log.info("取消订单：{}",id);
        orderService.cancelOrderBy(id);

        return Result.success();
    }

    /**
     * 再来一单
     *
     * @param id 订单id
     * @return
     */
    @ApiOperation("再来一单")
    @PostMapping("/repetition/{id}")
    public Result<String> oneMoreOrder(@PathVariable Integer id){
        log.info("再来一单");
        orderService.oneMoreOrder(id);

        return Result.success();
    }

    /**
     * 用户催单
     *
     * @param id
     * @return
     */
    @ApiOperation("用户催单")
    @GetMapping("/reminder/{id}")
    public Result<String> orderReminder(@PathVariable Long id){
        log.info("用户催单:{}",id);
        orderService.orderReminder(id);

        return Result.success();
    }

}
